<template>
  <div class="p-2">
    <transition :enter-active-class="proxy?.animate.searchAnimate.enter" :leave-active-class="proxy?.animate.searchAnimate.leave">
      <div v-show="showSearch" class="mb-[10px]">
        <el-card shadow="hover">
          <el-form ref="queryFormRef" :model="queryParams" :inline="true">
            <el-form-item label="登录地址" prop="ipaddr">
              <el-input v-model="queryParams.ipaddr" placeholder="请输入登录地址" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="用户名称" prop="nickName">
              <el-input v-model="queryParams.nickName" placeholder="请输入用户名称" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="状态" prop="status">
              <el-select v-model="queryParams.status" placeholder="登录状态" clearable>
                <el-option v-for="dict in sys_common_status" :key="dict.value" :label="dict.label" :value="dict.value*1" />
              </el-select>
            </el-form-item>
            <el-form-item label="时间" style="width: 308px">
              <el-date-picker
                v-model="dateRange"
                value-format="YYYY-MM-DD HH:mm:ss"
                type="daterange"
                range-separator="-"
                start-placeholder="开始日期"
                end-placeholder="结束日期"
                :default-time="[new Date(2000, 1, 1, 0, 0, 0), new Date(2000, 1, 1, 23, 59, 59)]"
              ></el-date-picker>
            </el-form-item>
            <el-form-item>
              <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
              <el-button icon="Refresh" @click="resetQuery">重置</el-button>
            </el-form-item>
          </el-form>
        </el-card>
      </div>
    </transition>

    <el-card shadow="hover">
      <template #header>
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button v-hasPermi="['monitor:logininfor:export']" type="warning" plain icon="Download" @click="handleExport">导出</el-button>
          </el-col>
          <right-toolbar v-model:showSearch="showSearch" @query-table="getList"></right-toolbar>
        </el-row>
      </template>

      <el-table
        ref="loginInfoTableRef"
        v-loading="loading"
        :data="loginInfoList"
        :default-sort="defaultSort"
        @sort-change="handleSortChange"
      >
    
        <el-table-column
          label="用户名称"
          align="center"
          prop="nickName"
          :show-overflow-tooltip="true"
          sortable="custom"
          :sort-orders="['ascending', 'descending']"
        />
        <el-table-column label="描述" align="center" prop="title" :show-overflow-tooltip="true" />
       
        <el-table-column label="操作类型" prop="businessType" align="center" :formatter="formatBusinessType">

        </el-table-column>
        <el-table-column label="地址" align="center" prop="operIp" :show-overflow-tooltip="true" />

        <el-table-column label="登录状态" align="center" prop="status">
          <template #default="scope">
            <dict-tag :options="sys_common_status" :value="scope.row.status" />
          </template>
        </el-table-column>
       
        <el-table-column label="访问时间" align="center" prop="createTime" sortable="custom" :sort-orders="['ascending', 'descending']"  width="180">
          <template #default="scope">
            <span>{{ parseTime(scope.row.createTime) }}</span>
          </template>
        </el-table-column>
      </el-table>

      <pagination v-show="total > 0" v-model:page="queryParams.page" v-model:limit="queryParams.size" :total="total" @pagination="getList" />
    </el-card>
  </div>
</template>

<script setup name="Logininfor" lang="ts">
import { list } from '@/api/monitor/loginInfo';
import { LoginInfoQuery, LoginInfoVO } from '@/api/monitor/loginInfo/types';

const { proxy } = getCurrentInstance() as ComponentInternalInstance;
const { sys_common_status } = toRefs<any>(proxy?.useDict('sys_common_status'));

const loginInfoList = ref<LoginInfoVO[]>([]);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref<Array<number | string>>([]);
const single = ref(true);
const multiple = ref(true);
const selectName = ref<Array<string>>([]);
const total = ref(0);
const dateRange = ref<[DateModelType, DateModelType]>(['', '']);
const defaultSort = ref<any>({ prop: 'create_time', order: 'desc' });

const queryFormRef = ref<ElFormInstance>();
const loginInfoTableRef = ref<ElTableInstance>();
// 查询参数
const queryParams = ref<LoginInfoQuery>({
  page: 1,
  size: 10,
  ipaddr: '',
  nickName: '',
  status: 0,
  sort: defaultSort.value.prop,
  order: defaultSort.value.order
});

/** 查询登录日志列表 */
const getList = async () => {
  loading.value = true;
  const res = await list(proxy?.addDateRange(queryParams.value, dateRange.value));
  loginInfoList.value = res.data.rows;
  total.value = parseInt(res.data.total);
  loading.value = false;
};
/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.value.page = 1;
  getList();
};
/** 重置按钮操作 */
const resetQuery = () => {
  dateRange.value = ['', ''];
  queryFormRef.value?.resetFields();
  queryParams.value.page = 1;
  loginInfoTableRef.value?.sort(defaultSort.value.prop, defaultSort.value.order);
};

/** 排序触发事件 */
const handleSortChange = (column: any) => {
  var sortName = column.prop;
  if(sortName == 'nickName'){
    sortName = 'nick_name';
  }else if(sortName == 'createTime'){
    sortName = 'create_time';
  }
  var orderName = column.order;
  if(orderName == 'ascending'){
    orderName = 'asc';
  }else if(orderName == 'descending'){
    orderName = 'desc';
  }
  queryParams.value.sort = sortName;
  queryParams.value.order = orderName;
  getList();
};


/** 导出按钮操作 */
const handleExport = () => {
  proxy?.download(
    'log/export/login',
    {
      ...queryParams.value
    },
    `config_${new Date().getTime()}.xlsx`
  );
};
//0其它 1新增 2修改3删除 4授权 5导出 6导入 7登录 8退出
const formatBusinessType =  (row) => {
  if(row.businessType == 0){
      return '其它';
  }else if(row.businessType == 1){
    return '新增';
  }else if(row.businessType == 2){
    return '修改';
  }else if(row.businessType == 3){
    return '删除';
  }else if(row.businessType == 4){
    return '授权';
  }else if(row.businessType == 5){
    return '导出';
  }else if(row.businessType == 6){
    return '导入';
  }else if(row.businessType == 7){
    return '登录';
  }else if(row.businessType == 1){
    return '退出';
  }
   
}



onMounted(() => {
  getList();
});
</script>
